package com.easylinkin.linkappapi.grid.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.grid.entity.GridAppraise;
import com.easylinkin.linkappapi.grid.entity.GridAppraiseCompany;
import com.easylinkin.linkappapi.grid.mapper.GridAppraiseCompanyMapper;
import com.easylinkin.linkappapi.grid.models.dto.GridAppraiseCompanyScoreDTO;
import com.easylinkin.linkappapi.grid.service.IGridAppraiseCompanyService;
import com.easylinkin.linkappapi.grid.service.IGridAppraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 网格评比单位表 服务实现类
 *
 * @author
 * @since 2024-05-16
 */
@Service
public class GridAppraiseCompanyServiceImpl extends ServiceImpl<GridAppraiseCompanyMapper, GridAppraiseCompany> implements IGridAppraiseCompanyService {

    @Autowired
    private IGridAppraiseService iGridAppraiseService;

    @Override
    public RestMessage queryList(Integer appraiseId) {
        List<GridAppraiseCompany> gridAppraiseCompanies = this.list(Wrappers.<GridAppraiseCompany>lambdaQuery()
                .eq(GridAppraiseCompany::getAppraiseId, appraiseId)
                .orderByDesc(GridAppraiseCompany::getTotalScore)
        );
        return RestBuilders.successBuilder().data(gridAppraiseCompanies).build();
    }

    @Override
    public RestMessage fillOutScore(GridAppraiseCompanyScoreDTO gridAppraiseCompanyScoreDTO) {
        GridAppraiseCompany appraiseCompany = this.getById(gridAppraiseCompanyScoreDTO.getId());
        if (ObjectUtil.isNull(appraiseCompany)) {
            return RestBuilders.errorBuilder().message("数据不存在").build();
        }
        GridAppraiseCompany gridAppraiseCompany = BeanUtil.copyProperties(gridAppraiseCompanyScoreDTO, GridAppraiseCompany.class);
        Integer total = 0;
        if(ObjectUtil.isNotNull(gridAppraiseCompany.getScheduleScore())){
            total += gridAppraiseCompany.getScheduleScore();
        }
        if(ObjectUtil.isNotNull(gridAppraiseCompany.getQualityScore())){
            total += gridAppraiseCompany.getQualityScore();
        }
        if(ObjectUtil.isNotNull(gridAppraiseCompany.getSafetyScore())){
            total += gridAppraiseCompany.getSafetyScore();
        }
        if(ObjectUtil.isNotNull(gridAppraiseCompany.getConstructionScore())){
            total += gridAppraiseCompany.getConstructionScore();
        }
        if(ObjectUtil.isNotNull(total)&&total>0){
            gridAppraiseCompany.setTotalScore(total);
        }
        boolean b = this.updateById(gridAppraiseCompany);
        if (b) {
            this.refreshCompanyCount(appraiseCompany.getAppraiseId());
            return RestBuilders.successBuilder().message("填写成功").build();
        }
        return RestBuilders.errorBuilder().message("填写失败").build();
    }

    @Override
    public void refreshCompanyCount(Integer appraiseId) {
        List<GridAppraiseCompany> gridAppraiseCompanies = this.list(Wrappers.<GridAppraiseCompany>lambdaQuery().eq(GridAppraiseCompany::getAppraiseId, appraiseId));
        int size = gridAppraiseCompanies.size();
        int count = 0;
        if (CollectionUtil.isNotEmpty(gridAppraiseCompanies)) {
            for (GridAppraiseCompany gridAppraiseCompany : gridAppraiseCompanies) {
                if (gridAppraiseCompany.getQualityScore() != null) {
                    count = count + 1;
                }
            }
        }
        GridAppraise gridAppraise = new GridAppraise();
        gridAppraise.setId(appraiseId);
        gridAppraise.setNeedCompanyCount(size);
        gridAppraise.setCompanyCount(count);
        iGridAppraiseService.updateById(gridAppraise);
    }

    @Override
    public Map<Integer, List<GridAppraiseCompany>> mapGroupByAppraiseId(Collection<Integer> appraiseIds) {
        List<GridAppraiseCompany> gridAppraiseCompanies = this.list(Wrappers.<GridAppraiseCompany>lambdaQuery()
                .in(GridAppraiseCompany::getAppraiseId, appraiseIds)
                .orderByDesc(GridAppraiseCompany::getTotalScore)
        );

        return gridAppraiseCompanies.stream().collect(Collectors.groupingBy(GridAppraiseCompany::getAppraiseId));
    }
}